﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Common;


public class ChatMessage
{
    public GUIModule Module;
    public bool Enable = false;

    private Rect[] tabButtons = null;
    private Rect cavanRect;
    private Rect chatRect;
    private string chatString = string.Empty;
    private Vector2 textScroll = Vector2.zero;
    private List<ChatItem> tmpHistory = new List<ChatItem>();
    private List<ChatItem> history = new List<ChatItem>();
    private float fontHeight = 0;
    private float chatWindowHeight = 0;
    GUILayoutOption[] ops;
    public ChatMessage(GUIModule module)
    {
        Module = module;
        Module.OnScreenChange += delegate(int width, int height)
        {
            calculateSize();
        };
        RegionStateManager.LoginStateChangeHandle += delegate(LoginState form, LoginState to)
        {
            Enable = true;
        };
    }
    void calculateSize()
    {
        fontHeight = AssetHelp.GetFontWidth(GuiStyles.ChatMessage, "test").y;
        cavanRect = new Rect(20, Screen.height - 300, 400, 300);
        chatRect = new Rect(20, Screen.height - 300, 400, 270);
        tabButtons = new Rect[] { 
                new Rect(cavanRect.xMin, cavanRect.yMin, cavanRect.width, cavanRect.height - 30),
                new Rect(cavanRect.xMin, cavanRect.yMin + 275 , 60, 20),
                new Rect(cavanRect.xMin + 65, cavanRect.yMin + 275 , 270, 20),
                new Rect(cavanRect.xMin + 65 + 275, cavanRect.yMin + 275 , 60, 20)
            };
        ops = new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Width(chatRect.width), GUILayout.MinWidth(0) };
    }
    public void Chat(ChatType type, string name, string text)
    {
        if (Enable || string.IsNullOrEmpty(text)) return;
        ChatItem item = new ChatItem(type, "[" + name + "]:" + text);
        history.Add(item);

        float maxWidth = chatRect.width;
        int index = 0;
        string msg = item.Chat;
        int length = msg.Length;
        while (length > index)
        {
            string tmpMsg = msg.Substring(index, length - index);
            if (string.IsNullOrEmpty(tmpMsg)) break;

            float tmpWidth = AssetHelp.GetFontWidth(GuiStyles.ChatMessage, tmpMsg).x;
            //Log.Debug("width:" + tmpWidth + ",max:" + maxWidth + ",msg:" + tmpMsg);
            if (tmpWidth < maxWidth)
            {
                item = new ChatItem(type, tmpMsg);
                tmpHistory.Add(item);

                index = length;
                length = msg.Length;
            }
            else
            {
                length--;
            }
        }
        chatWindowHeight = tmpHistory.Count * fontHeight;
        if (chatWindowHeight > 300) chatWindowHeight = 300;
        chatRect.yMin = Screen.height - chatWindowHeight - 30;
        textScroll.y += fontHeight;
    }
    public void OnGUI()
    {
        if (Enable)
        {
            GUILayout.BeginArea(chatRect);
            textScroll = GUILayout.BeginScrollView(textScroll, GuiStyles.ChatScroll);
            for (int i = 0; i < tmpHistory.Count; i++)
            {
                GUILayout.BeginHorizontal(ops);
                GUILayout.Label(tmpHistory[i].Chat, GuiStyles.ChatMessage);
                GUILayout.EndHorizontal();
            }
            GUILayout.EndScrollView();
            GUILayout.EndArea();

            //GUIControl.ChatMessageHistory(tabButtons[0], msgString);
            if (GUI.Button(tabButtons[1], "世界"))
            {

            }
            GUI.SetNextControlName("chat");
            chatString = GUI.TextField(tabButtons[2], chatString);
            if (GUI.Button(tabButtons[3], "发送"))
            {
                Chat(ChatType.Region, "You", chatString);
                Module.SendChat(ChatType.Region, AppInterface.AvatarModule.IOwner.Name, chatString);
                chatString = string.Empty;
            }
            if (string.Compare(GUI.GetNameOfFocusedControl(), "chat") == 0)
                InputManager.CanInput = false;
            else
                InputManager.CanInput = true; ;

            //好友，公会，世界，系统
        }
    }
}
public class ChatItem
{
    public ChatItem(ChatType type, string chat)
    {
        Type = type;
        Chat = chat;
    }
    public ChatType Type;
    public string Chat;
}
[Flags]
public enum ChatType
{
    Friends = 0,
    Groups,
    Region,
    System
}
